//
//  MCActivityStatisticsCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/25.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCActivityStatisticsCellClosure = (UIViewController) -> Void

class MCActivityStatisticsCell: UITableViewCell {

    public var closure : MCActivityStatisticsCellClosure?
    
    
    var model = MCActivityStatisticsModel() {
        didSet {
            nameLabel.text = model.goodsName
            countLabel.text = model.goodsNum.stringValue
            explainButton.setTitle(MCString_judgemantDecimalsIsZero(figure: model.totalFee.intValue), for: .normal)
        }
    }
    public var activityLogId = 0
    public var goodsCount = 0
    public var expressDelivery : NSNumber = 0 // 快递送的数量
    public var hasWriteOff : NSNumber = 0     // 已核销
    public var notWriteOff : NSNumber = 0     // 未核销
    public var allGoodsCount : NSNumber = 0   // 所有商品的数量
    public var allGoodsValue : NSNumber = 0   // 所有的营业额

    
    public var indexPath = IndexPath() {
        didSet {
            if indexPath.section == 0 {
                explainButton.isEnabled = false

                if indexPath.row == goodsCount {
                    nameLabel.text = "合计"
                    countLabel.text = allGoodsCount.stringValue
                    explainButton.setTitle(MCString_judgemantDecimalsIsZero(figure: allGoodsValue.intValue), for: .normal)
                    nameLabel.textColor = UIColor.MCMain
                    countLabel.textColor = UIColor.MCMain
                    explainButton.setTitleColor(UIColor.MCMain, for: .normal)
                } else {
                    nameLabel.textColor = UIColor.MCWords
                    countLabel.textColor = UIColor.MCWords
                }
                
            } else {
                explainButton.isEnabled = true
                explainButton.setTitle("查看", for: .normal)
                explainButton.setTitleColor(UIColor.blue, for: .normal)
                
                
                if indexPath.row == 0 {
                    nameLabel.text = "待核销"
                    nameLabel.textColor = UIColor.MCWords
                    countLabel.textColor = UIColor.MCWords
                    countLabel.text = notWriteOff.stringValue
                } else if indexPath.row == 1 {
                    nameLabel.text = "已核销"
                    nameLabel.textColor = UIColor.MCWords
                    countLabel.textColor = UIColor.MCWords
                    countLabel.text = hasWriteOff.stringValue
                } else if indexPath.row == 2 {
                    nameLabel.text = "快递送"
                    nameLabel.textColor = UIColor.MCWords
                    countLabel.textColor = UIColor.MCWords
                    countLabel.text = expressDelivery.stringValue
                }  else {
                    nameLabel.text = "合计"
                    nameLabel.textColor = UIColor.MCMain
                    countLabel.textColor = UIColor.MCMain
                    countLabel.text = String(expressDelivery.intValue + hasWriteOff.intValue + notWriteOff.intValue)
                }
                
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
   
        backgroundColor = UIColor.MCGray_light

        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(0)
            make.bottom.equalTo(-1)
            make.width.equalTo(MCScreenWidth*0.6 - 1.5)
        }
        
        self.addSubview(countLabel)
        countLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(0)
            make.bottom.equalTo(-1)
            make.left.equalTo(nameLabel.snp.right).offset(1.5)
            make.width.equalTo(MCScreenWidth*0.2 - 1.5)
        }

        self.addSubview(explainButton)
        explainButton.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(0)
            make.bottom.equalTo(-1)
            make.right.equalTo(self)
            make.width.equalTo(MCScreenWidth*0.2)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    @objc func explainButtonClikced() {
        
        if indexPath.section == 1 {
            let vc = MCOrderListViewController()
            vc.activityLogId = activityLogId
            vc.status = 0
        
            if indexPath.row == 3 {
                vc.writeOffStatus = 0
            } else {
                vc.writeOffStatus = indexPath.row + 1
            }
            
            closure?(vc)
        }        
    }
    
    lazy var nameLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(13)
        label.textAlignment = NSTextAlignment.center
        label.backgroundColor = UIColor.white
        return label
    }()
    
    lazy var countLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(13)
        label.textAlignment = NSTextAlignment.center
        label.backgroundColor = UIColor.white
        label.text = "0"
        return label
    }()
    

    lazy var explainButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(13)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.backgroundColor = UIColor.white
        button.setTitle("0", for: .normal)
        button.addTarget(self, action: #selector(explainButtonClikced), for: .touchUpInside)
        return button
    }()
    
}
